package com.itheima.reggie.interceptor;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.reggie.JwtUtil;
import com.itheima.reggie.ResultInfo;
import com.itheima.reggie.UserHolder;
import com.itheima.reggie.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * 拦截器
 */
@Component
@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //1.从请求头中获取token
        String token = request.getHeader("Authorization");
        token = token.replace("Bearer", "").trim();
        log.info("token = " + token);

        if (StringUtils.isEmpty(token)) {
            //为空返回错误提示
            ResultInfo resultInfo = ResultInfo.error("NOTLOGIN");
            String json = JSON.toJSONString(resultInfo);
            response.getWriter().write(json);
            return false;
        }

        //2.解析token
        try {
            JwtUtil.parseToken(token);
        } catch (Exception e) {
            //返回错误提示
            ResultInfo resultInfo = ResultInfo.error("NOTLOGIN");
            String json = JSON.toJSONString(resultInfo);
            response.getWriter().write(json);
            return false;//禁止通行
        }

        //3.从redis中查询token
        User user = (User) redisTemplate.opsForValue().get("TOKEN_" + token);
        if (user == null) {
            //返回错误提示
            ResultInfo resultInfo = ResultInfo.error("NOTLOGIN");
            String json = JSON.toJSONString(resultInfo);
            response.getWriter().write(json);
            return false; //禁止通行
        }


        //4.到这就没问题
        //token续期
        redisTemplate.opsForValue().set("TOKEN" + token, user, 1, TimeUnit.DAYS);
        //将用户保存到 ThreadLocal中
        UserHolder.set(user);
        //放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserHolder.remove();
    }
}
